<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 10:32
 */

namespace app\api\service\partner;


use app\api\model\Assistant;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\Member;
use app\api\model\ProvincePartner;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class AssistantSer extends Token
{
    // 用户信息
    private $user;

    // 用户会员信息
    private $member;

    // 合伙人信息
    private $partner;

    // 类型
    private $status;

    function __construct()
    {
        self::judgeMember();
        $uid = self::getCurrentUid();
        $this->member = Member::where('user_id',$uid)->find();
        $this->user = User::get($uid);
        if($this->user['partner_status'] == 0 || $this->user['partner_status'] == 4) {
            throw new ParameterException([
                'msg' => [
                    'assistant' => '您还不是合伙人'
                ]
            ]);
        }
    }

    // 入口文件
    public function entrance()
    {
        return $this->judgeData();
    }

    // 判断数据
    private function judgeData()
    {
        if($this->user['partner_status'] == 1) {
            // 省级
            $this->partner = ProvincePartner::where('user_id',$this->user['id'])->find();
            if(!$this->partner) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '您还不是合伙人'
                    ]
                ]);
            }
        } elseif($this->user['partner_status'] == 2) {
            // 市级
            $this->partner = CityPartner::where('user_id',$this->user['id'])->find();
            if(! $this->partner) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '您还不是合伙人'
                    ]
                ]);
            }
        } elseif($this->user['partner_status'] == 3) {
            // 区县
            $this->partner = CountyPartner::where('user_id',$this->user['id'])->find();
            if(! $this->partner) {
                throw new ParameterException([
                    'msg' => [
                        'assistant' => '您还不是合伙人'
                    ]
                ]);
            }
        }
        return $this->getData();
    }

    // 获取数据 并返回
    private function getData()
    {
        return Assistant::where('type',$this->user['partner_status'])
            ->where('pid',$this->partner['id'])
            ->select()->toArray();
    }
}